package com.huawei.esdk.tp.northcommu.cxf.professional;

import org.apache.log4j.Logger;

import com.huawei.esdk.platform.common.SDKErrorCode;
import com.huawei.esdk.platform.common.SDKResult;
import com.huawei.esdk.platform.common.ThreadLocalHolder;
import com.huawei.esdk.platform.common.exception.SDKException;
import com.huawei.esdk.platform.listener.ISessionListener;
import com.huawei.esdk.platform.listener.ISessionListenerManager;
import com.huawei.esdk.tp.authorize.AuthenticateService;
import com.huawei.esdk.tp.authorize.LoginRequestBean;
import com.huawei.esdk.tp.authorize.SessionManager;
import com.huawei.esdk.tp.common.ErrInfo;
import com.huawei.esdk.tp.common.constants.InfoKeys;
import com.huawei.esdk.tp.northcommu.cxf.professional.gen.Authenticate;
import com.huawei.esdk.tp.northcommu.cxf.professional.gen.AuthenticateResponse;
import com.huawei.esdk.tp.northcommu.cxf.professional.gen.KeepAlive;
import com.huawei.esdk.tp.northcommu.cxf.professional.gen.KeepAliveResponse;
import com.huawei.esdk.tp.northcommu.cxf.professional.gen.LoginRequest;
import com.huawei.esdk.tp.northcommu.cxf.professional.gen.LoginRequestResponse;
import com.huawei.esdk.tp.northcommu.cxf.professional.gen.Logout;
import com.huawei.esdk.tp.northcommu.cxf.professional.gen.LogoutResponse;
import com.huawei.esdk.tp.northcommu.cxf.professional.gen.TPCommon;

public class TPProfessionalCommonImpl implements TPCommon
{
    private static final Logger LOGGER = Logger.getLogger(TPProfessionalCommonImpl.class);
    
    private AuthenticateService authorizeService;
    
    private ISessionListener sessionListener;
    
    private ISessionListenerManager sessionListenerManager;
    
    public void init()
    {
        sessionListenerManager.registerListner(sessionListener);
    }
    
    public AuthenticateService getAuthorizeService()
    {
        return authorizeService;
    }
    
    public ISessionListener getSessionListener()
    {
        return sessionListener;
    }
    
    public void setAuthorizeService(AuthenticateService authorizeService)
    {
        this.authorizeService = authorizeService;
    }
    
    public void setSessionListener(ISessionListener sessionListener)
    {
        this.sessionListener = sessionListener;
    }
    
    public ISessionListenerManager getSessionListenerManager()
    {
        return sessionListenerManager;
    }
    
    public void setSessionListenerManager(ISessionListenerManager sessionListenerManager)
    {
        this.sessionListenerManager = sessionListenerManager;
    }
    
    @Override
    public LogoutResponse logout(Logout parameters)
    {
        LogoutResponse response = new LogoutResponse();
        try
        {
            if (null == parameters)
            {
                response.setResultCode(ErrInfo.SDK_PARAM_NOT_COMPLETE_ERRORCODE);
                return response;
            }
            SDKErrorCode result = authorizeService.appLogoutRequest();
            response.setResultCode((int)result.getErrCode());
        }
        catch (Exception e)
        {
            LOGGER.error("", e);
            response.setResultCode(ErrInfo.SDK_SYSTEM_ERRORCODE);
        }
        
        return response;
    }
    
    @Override
    public KeepAliveResponse keepAlive(KeepAlive parameters)
    {
        KeepAliveResponse response = new KeepAliveResponse();
        try
        {
            if (null == parameters)
            {
                response.setResultCode(ErrInfo.SDK_PARAM_NOT_COMPLETE_ERRORCODE);
                return response;
            }
            SDKErrorCode result = authorizeService.keepAlive();
            response.setResultCode((int)result.getErrCode());
        }
        catch (Exception e)
        {
            LOGGER.error("keepAlive method error", e);
            response.setResultCode(ErrInfo.SESSIONID_ERRORCODE);
        }
        return response;
    }
    
    @Override
    public AuthenticateResponse authenticate(Authenticate parameters)
    {
        AuthenticateResponse response = new AuthenticateResponse();
        try
        {
            if (null == parameters || 0 == parameters.getEncryptPassword().length)
            {
                response.setResultCode(ErrInfo.SDK_PARAM_NOT_COMPLETE_ERRORCODE);
                return response;
            }
            
            SDKErrorCode result = authorizeService.appAuthenticate(parameters.getEncryptPassword());
            response.setResultCode((int)result.getErrCode());
        }
        catch (SDKException e)
        {
            LOGGER.error("authenticate method error", e);
            response.setResultCode((int)e.getSdkErrCode());
        }
        catch (Exception e)
        {
            LOGGER.error("authenticate method error", e);
            response.setResultCode(ErrInfo.SDK_SYSTEM_ERRORCODE);
        }
        
        return response;
    }
    
    @Override
    public LoginRequestResponse loginRequest(LoginRequest parameters)
    {
        LoginRequestResponse response = new LoginRequestResponse();
        SessionManager sessionMgr = SessionManager.getInstance();
        String sessionId = (String)ThreadLocalHolder.get().getEntities().get(InfoKeys.SDK_SESSION_ID.toString());
        try
        {
            //首先判断是否是重复登录
            if (sessionMgr.isSDKSessionExists(sessionId) && sessionMgr.getSDKSession(sessionId).isLogged())
            {
                response.setResultCode(ErrInfo.LOGIN_REPETITIVE_ERRORCODE);
                return response;
            }
            
            //保存Session信息
            if (!sessionMgr.saveSDKSession(sessionId))
            {
                response.setResultCode(ErrInfo.SDK_SYSTEM_ERRORCODE);
                return response;
            }
            
            //校验参数
            if (null == parameters)
            {
                response.setResultCode(ErrInfo.SDK_PARAM_NOT_COMPLETE_ERRORCODE);
                return response;
            }
            
            SDKResult<LoginRequestBean> result = authorizeService.appLoginRequest(parameters.getUserName());
            response.setHashType(result.getResult().getHashType());
            response.setRandomSequence(result.getResult().getRandomkey());
            response.setResultCode((int)result.getErrCode());
        }
        catch (Exception e)
        {
            LOGGER.error("loginRequest method error", e);
            response.setResultCode(ErrInfo.SDK_SYSTEM_ERRORCODE);
        }
        
        return response;
    }
}
